package com.architecture.core.service.query;

import com.architecture.core.entity.Permission;
import com.architecture.core.entity.PermissionRole;
import com.architecture.core.except.ErrException;
import com.architecture.core.service.AbstractService;
import com.architecture.core.utils.ErrCode;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: aguang
 * Date: 13-1-21
 * Time: 下午6:10
 * To change this template use File | Settings | File Templates.
 */
@Service("queryPermissionService")
public class QueryPermissionService extends AbstractService {

    @Resource
    private QueryPermissionRoleService queryPermissionRoleService;

    public List<Permission> findPermissionsByPermissionRoleId(String permissionRoleId) throws ErrException {

        try {
            return this.findAllPermission(queryPermissionRoleService.findPermissionRolesByPermissionRoleId(permissionRoleId));
        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    public List<Permission> findPermissionsByUserId(String userId) throws ErrException {
        try {
            return this.findAllPermission(queryPermissionRoleService.findPermissionRolesByUserId(userId));
        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    public List<Permission> findPermissionsByUserId(String userId, int categoryType) throws ErrException {

        try {
            return this.findAllPermission(queryPermissionRoleService.findPermissionRolesByUserId(userId, categoryType));
        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    public List<Permission> findPermissionsByCategoryId(String categoryId) throws ErrException {

        try {
            return this.findAllPermission(queryPermissionRoleService.findPermissionRolesByCategoryId(categoryId));
        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    public List<Permission> findPermissionsByGroupId(String groupId) throws ErrException {
        try {
            return this.findAllPermission(queryPermissionRoleService.findPermissionRolesByGroupId(groupId));

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    public List<Permission> findPermissionsByGroupId(String groupId, String userId) throws ErrException {

        try {
            return this.findAllPermission(queryPermissionRoleService.findPermissionRolesByGroupId(groupId,userId));

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    public List<Permission> findPermissionsByGroupId(String groupId, int categoryType) throws ErrException {
        try {
            return this.findAllPermission(queryPermissionRoleService.findPermissionRolesByGroupId(groupId, categoryType));

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    public List<Permission> findPermissionsByGroupId(String groupId, String userId, int categoryType) throws ErrException {
        try {
            return this.findAllPermission(queryPermissionRoleService.findPermissionRolesByGroupId(groupId, userId, categoryType));

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    public List<Permission> findPermissionsByPermissionRoleId(String permissionRoleId,Integer firstIndex, Integer fetchSize) throws ErrException {

        try {
            return this.findPermissionsByPermissionRoleId(permissionRoleId).subList(firstIndex,firstIndex+fetchSize);
        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    public List<Permission> findPermissionsByUserId(String userId,Integer firstIndex, Integer fetchSize) throws ErrException {
        try {

            return this.findPermissionsByUserId(userId).subList(firstIndex,firstIndex+fetchSize);
        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    public List<Permission> findPermissionsByUserId(String userId, int categoryType,Integer firstIndex, Integer fetchSize) throws ErrException {

        try {
            return this.findPermissionsByUserId(userId,categoryType).subList(firstIndex,firstIndex+fetchSize);
        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    public List<Permission> findPermissionsByCategoryId(String categoryId,Integer firstIndex, Integer fetchSize) throws ErrException {

        try {
            return this.findPermissionsByCategoryId(categoryId).subList(firstIndex,firstIndex+fetchSize);
        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    public List<Permission> findPermissionsByGroupId(String groupId,Integer firstIndex, Integer fetchSize) throws ErrException {
        try {
            return this.findPermissionsByGroupId(groupId).subList(firstIndex,firstIndex+fetchSize);

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    public List<Permission> findPermissionsByGroupId(String groupId, String userId,Integer firstIndex, Integer fetchSize) throws ErrException {

        try {
            return this.findPermissionsByGroupId(groupId,userId).subList(firstIndex,firstIndex+fetchSize);

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    public List<Permission> findPermissionsByGroupId(String groupId, int categoryType,Integer firstIndex, Integer fetchSize) throws ErrException {
        try {
            return this.findPermissionsByGroupId(groupId,categoryType).subList(firstIndex,firstIndex+fetchSize);

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    public List<Permission> findPermissionsByGroupId(String groupId, String userId, int categoryType,Integer firstIndex, Integer fetchSize) throws ErrException {
        try {
            return this.findPermissionsByGroupId(groupId,userId,categoryType).subList(firstIndex,firstIndex+fetchSize);

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    public List<Permission> findPermissionsByPermissionRoleId(String permissionRoleId,Integer firstIndex) throws ErrException {

        try {
            List<Permission> permissionList =  this.findPermissionsByPermissionRoleId(permissionRoleId);
            return permissionList.subList(firstIndex,permissionList.size());
        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    public List<Permission> findPermissionsByUserId(String userId,Integer firstIndex) throws ErrException {
        try {

            List<Permission> permissionList = this.findPermissionsByUserId(userId);

            return permissionList.subList(firstIndex,permissionList.size());
        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    public List<Permission> findPermissionsByUserId(String userId, int categoryType,Integer firstIndex) throws ErrException {

        try {
            List<Permission> permissionList = this.findPermissionsByUserId(userId,categoryType);
            return permissionList.subList(firstIndex,permissionList.size());
        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    public List<Permission> findPermissionsByCategoryId(String categoryId,Integer firstIndex) throws ErrException {

        try {
            List<Permission> permissionList =  this.findPermissionsByCategoryId(categoryId);
            return permissionList.subList(firstIndex,permissionList.size());
        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    public List<Permission> findPermissionsByGroupId(String groupId,Integer firstIndex) throws ErrException {
        try {
            List<Permission> permissionList = this.findPermissionsByGroupId(groupId);
            return permissionList.subList(firstIndex,permissionList.size());
        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    public List<Permission> findPermissionsByGroupId(String groupId, String userId,Integer firstIndex) throws ErrException {

        try {
            List<Permission> permissionList =  this.findPermissionsByGroupId(groupId,userId);
            return permissionList.subList(firstIndex,permissionList.size());

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    public List<Permission> findPermissionsByGroupId(String groupId, int categoryType,Integer firstIndex) throws ErrException {
        try {
            List<Permission> permissionList =  this.findPermissionsByGroupId(groupId,categoryType);
            return permissionList.subList(firstIndex,permissionList.size());
        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    public List<Permission> findPermissionsByGroupId(String groupId, String userId, int categoryType,Integer firstIndex) throws ErrException {
        try {
            List<Permission> permissionList =  this.findPermissionsByGroupId(groupId,userId,categoryType);
            return permissionList.subList(firstIndex,permissionList.size());
        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    private List<Permission> findAllPermission(List<PermissionRole> roleList) {
        List<Permission> permissionList = new ArrayList<Permission>();
        for (PermissionRole role : roleList) {
            List<Permission> addPermissionList = role.getPermissionList();
            for (Permission permission : addPermissionList) {
                if (permission.isDeleted()) {
                    continue;
                } else {
                    permissionList.add(permission);
                }
            }
        }
        return permissionList;
    }

}
